<?php
class Sql {
	
	/**
	 * MySQLi connection object
	 *
	 * @var MySQLi object
	 */
	protected $_connection;
	
	/**
	 * Object instance
	 *
	 * @var Sql
	 */
	private static $_instance;
	
	/**
	 * Instance of singleton object
	 *
	 * @return Sql
	 */
	public static function getInstance () {
		if(self::$_instance === null) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function getConnection() {
		$this->_connection = new mysqli(Config::$db['host'], Config::$db['user'], Config::$db['password'], Config::$db['dbname']);
		$this->_connection->set_charset('utf8');
	}
	
	/**
	 * Execute query
	 *
	 * @param string $qeryString
	 * @return array
	 */
	public function query($qeryString) {
		$resultArray = array();
		if(!empty($qeryString)) {
			$result = $this->_connection->query(trim($qeryString));
			if($result && is_object($result)) {
				while ($row = $result->fetch_object()) {
					$resultArray[] = $row;
				}
				return $resultArray;
			}
			else {
				return $result;
			}
			throw new Exception("Query failed");
		}
		throw new Exception('Query string undefined');
	}
	
	public function getLastInsertId() {
		return $this->_connection->insert_id;  
	}
}